/**
 * 部门控制js
 */
$(function(){
	var openWindow=window.document;
	//var searchHeight=$("#divform").height();
	$("#listCourt").height($(openWindow).height()-$(openWindow).height()*0.05-85);
	setInterval(function(){
		$("#listCourt").width(window.parent.$("#centerIframe").width()-124);
	},1000)
	$("#all").addClass("a_hover");
	//动态设置改变操作列宽度
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length)*fcolMinWidth;
	}
	$('#listCourt').datagrid({
		url:$Path+'/admin/court/findCourt',  
		title:'法庭列表',
		method:'get',
		pagination:true,
		fitColumns:true,
		pageList: [15,30,50,200],
		loadMsg:'加载中......',
		sortName:"courtid",
		sortOrder:"desc",
		rowStyler:function(index,row){   
	        if (row.status==2){   
	        	return 'background-color:'+window.flagColor;
	        }   
	    }, 
	    columns:[[
			{field:'ck',checkbox:true},
	        {field:'courtname',sortable:true,title:'法庭名称',width:140,align:'center',},  
	        {field:'status',sortable:true,title:'状态',align:'center',width:80,formatter:function(value,row,index){
	        	if(value == 1){
	        		return '启用';
	        	}else if(value == 2){
	        		return '停用';
	        	}else if(value == 3){
	        		return "使用中";
	        	}else if(value == 4){
	        		return "未使用";
	        	}
	        }},
	        {field:'microCount',align:'center',title:'麦克风数量',width:100},
	        {field:'createtime',sortable:true,title:'创建时间',width:140,formatter:function(value,row,index){
				 if(value==''){
	        		 return '';
	        	 }
	        	 var datetime=Common.formatterDate2(value);
	        	 return datetime;
			 }},
			{field:'createname',sortable:true,title:'创建人',width:80},
			{field:'id' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
				 var re=new RegExp("{param}","g");
				 var ref=opt.replace(re,row.courtid);
				 return ref;
			}},
			
	    ]]  
	});
	//如果操作项为空隐藏操作列
	if(opt==''){
		$("#listCourt").datagrid('hideColumn','id');
	}
	//绑定顶部操作按钮
	$("#listCourt").datagrid({
		toolbar: '#tb'
	});
	var data=[{"id":"","text":"全部"},
	          {"id":"1","text":"启用"},
	          {"id":"2","text":"停用"},
	          {"id":"3","text":"使用中"},
	          {"id":"4","text":"未使用"}
	         ]
	$("#searchsort").combobox({
		 data:data,
		 valueField:'id',
		 textField:'text',
		 editable:false,
		 panelHeight:"auto",
		 onLoadSuccess:function(data){
			 $("#searchsort").combobox('select',data[0].id);
		 },
		 onSelect:function(data){
			 var vo={
						"drCourtVo.courtName" :　$("#courtName").val(),
						"drCourtVo.status" :data.id
				};
				$("#listCourt").datagrid('load',vo);
		 }
	 })
})

function search(){
	var vo={
			"drCourtVo.courtName" :　$("#courtName").val(),
			"drCourtVo.status" :　$("#searchsort").combobox('getValue')
	};
	$("#listCourt").datagrid('load',vo);
}
/**
 * 添加
 */
function add(){
	window.parent.openDialog("添加法庭信息",'/admin/court/addCourtDlg','',900,450,'mode=add');
}
/**
 * 修改
 */
function edit(id){
	window.parent.openDialog("修改法庭信息",'/admin/court/updateCourtDlg','',900,450,'mode=update&courtid='+id);
}
/**
 * 详情
 */
function view(id){
	window.parent.openDialog("查看法庭信息详情",'/admin/court/countView','',550,450,'courtid='+id);
}

function microPhone(id){
	window.parent.openDialog("编辑麦克风列表",'/admin/micro/findMicroByCourt','',400,510,'courtid='+id);
}
/**
 * 批量启用
 */
function betchUnFlag(){
	var rows=$("#listCourt").datagrid('getSelections');
	if(rows.length<=0){
		parent.$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	parent.$.messager.confirm('启用','您确定要启用选中的法庭吗？',function(data){
		if(data){
			var ids="'";
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].courtid+"','";
			}
			ids = ids.substring(0,ids.length-2)
			var authenticityToken=$("input[name='authenticityToken']").val();
			$.ajax({
				type:'post',
				url: $Path+'/admin/court/betchEnable',
				data:{ids:ids,authenticityToken:authenticityToken},
				success:function(data){
					if(data.code==200){
						parent.$.messager.alert("成功","启用成功",'info',function(){
							$("#listCourt").datagrid('load',null);
						});
					}
					else{
						parent.$.messager.alert("失败","启用失败："+data.msg,'error');
					}
				},
				error:function(data){
					parent.$.messager.alert("失败","启用失败：",'error');
				}
			});
		}
	})
}
/**
 * 批量停用
 */
function betchFlag(){
	var rows=$("#listCourt").datagrid('getSelections');
	if(rows.length<=0){
		parent.$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	parent.$.messager.confirm('停用','您确定要停用选中的法庭吗？',function(data){
		if(data){
			var ids="'";
			for(var i=0;i<rows.length;i++){
				if(rows[i].status == 3){
					parent.$.messager.alert('错误','选中的记录中含有正在使用的法庭!','error');
					return;
				}
				ids+=rows[i].courtid+"','";
			}
			ids = ids.substring(0,ids.length-2)
			var authenticityToken=$("input[name='authenticityToken']").val();
			$.ajax({
				type:'post',
				url: $Path+'/admin/court/betchDisable',
				data:{ids:ids,authenticityToken:authenticityToken},
				success:function(data){
					if(data.code==200){
						parent.$.messager.alert("成功","停用成功",'info',function(){
							$("#listCourt").datagrid('load',null);
						});
					}
					else{
						parent.$.messager.alert("失败","停用失败："+data.msg,'error');
					}
				},
				error:function(data){
					parent.$.messager.alert("失败","停用失败：",'error');
				}
			});
		}
	})
}
